package com.sinodata.bsm.cicp.finder.oracle;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

/**
 *
 * <p>
 * Description: 获取脚本中的参数,以及扩展其他方法
 * </p>
 *
 * @author Chao.Liu
 * @version 3.1

 * <p>
 * History:
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2011-11-9 下午3:48:07          Chao.Liu        3.1         To create
 * </p>
 *
 * @since
 * @see
 */
public class OracleFinderContants {
    private static final Logger logger = Logger.getLogger(OracleFinderContants.class);

    /**
     * 分割finder后面的参数<br>
     * 使用正则来分割参数
     * @param expression
     * @param splitParameters
     * @param splitKey
     * @return
     */
    public static Map<String, String> getArgument(String expression, String splitParameters, String splitKey) {
        Map<String, String> map = new HashMap<String, String>();

        if (null != expression || !"".equals(expression)) {
            if (null != splitParameters && null != splitKey) {
                try {
                    StringBuffer sb = new StringBuffer("");
                    sb.append("\\");
                    sb.append(splitParameters);
                    sb.append("([^\\");
                    sb.append(splitParameters);
                    sb.append("]{1,}[");
                    sb.append(splitKey);
                    sb.append("][^\\");
                    sb.append(splitParameters);
                    sb.append("]{0,})");
                    Pattern p = Pattern.compile(sb.toString());
                    Matcher m = p.matcher(expression);

                    int index = 0;
                    while (m.find(index)) {
                        String[] arg = m.group(1).split(splitKey);
                        String key = arg[0];
                        String value = arg[1];
                        map.put(key, value);
                        index = m.end();
                    }
                } catch (Exception e) {
                    logger.info("Separator is error by 'OracleFinderContants.getArgument'.");
                }
            } else {
                logger.info("Separator is not present by 'OracleFinderContants.getArgument'.");
            }
        } else {
            logger.info("Parameter is not present by 'OracleFinderContants.getArgument'.");
        }

        return map;
    }

    /**
     * 用[-]分割键值.然后用[:]将键值分开存储在Map中
     * @param expression
     * @return
     */
    public static Map<String, String> getArgument(String expression) {
        Map<String, String> map = new HashMap<String, String>();

        map = getArgument(expression, "$", ":");

        return map;
    }

    /**
     * 用[-]分割键值.然后用[空格]将键值分开存储在Map中
     * @param expression
     * @return
     */
    public static Map<String, String> getDefaultArgument(String expression) {
        Map<String, String> map = new HashMap<String, String>();

        map = getArgument(expression, "-", " ");

        return map;
    }

    public static Map<String, String> getArgMap(String expression) {
        Map<String, String> map = new HashMap<String, String>();

        map = getDefaultArgument(expression);
        if (map.isEmpty()) {
            logger.info("Not find argument by 'OracleFinderContants.getDefaultArgument(String)'.");
            logger.info("Start use 'OracleFinderContants.getArgument(String)'.");
            map = getArgument(expression);
        }

        if (map.isEmpty()) {
            logger.info("Not find argument by 'OracleFinderContants.getArgument(String)'.");
            logger.info("Plase check you finder argument.");
        }

        return map;
    }

    public static void main(String[] args) {
        String expression = "asdf";
        //        String expression = "$NAME:Oracle 无效 信息$QQ:756615797$ABC";
        //        String expression = "-NAME Oracle无效信息-QQ 756615797-ABC";
        Map<String, String> map = OracleFinderContants.getArgMap(expression);
        System.out.println(map.get("NAME"));
        System.out.println(map.get("QQ"));
    }
}
